<?php
/**
 * Created by PhpStorm.
 * User: robot
 * Date: 2018-04-27
 * Time: 00:43
 */

namespace app\admin\controller;


use app\base\BaseController;
use app\base\validate\BooksValidate;
use think\Request;
use app\base\model\Books as BooksModel;
use app\base\model\Classify as fenlei;

class Books extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }

    public function ajax()
    {


        $request = Request::instance();
        if (!$request->isAjax()) {
            return ['code'=>-1,'msg'=>'请求方式错误'];
        }

        $books = new BooksModel();
        $limit = $request->param('limit');
        $offset = $limit * $request->param('page')-$limit;

        $total = $books->count('book_id');
        $data = $books->limit($offset,$limit)->select();
        return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $data];
    }

    public function dell()
    {
        $request = Request::instance();
        $bookid = $request->has('book_id')?$request->param('book_id'):false;
        if (!$bookid) {
            return ['code' => -1, 'msg' => '参数错误'];
        }
        $book_img=BooksModel::get($bookid)->book_img;
        if ($book_img!='/static/books-img/books.jpg') {
            unlink(ROOT_PATH.'public'.$book_img);
        }


        if (BooksModel::destroy($bookid)) {
            return ['code' => 1, 'msg' => '删除成功'];
        } else {
            return ['code' => -1, 'msg' => '删除失败'];
        }
    }


    public function addBooks()
    {
        $request = Request::instance();
        if ($request->isAjax()) {
            $data = $request->param();
            $validate = new BooksValidate();
            if (!$validate->check($data)) {
                return ['code'=>-1,'msg'=>$validate->getError()];
            }

            $data['book_dec'] = isset($data['book_dec']) ? $data['book_dec'] : '';
            if (!isset($data['classify'] )) {
                return ['code'=>-1,'msg'=>'请确认书籍分类'];
            }


            BooksModel::create([
                'book_name' => $data['book_name'],
                'book_auth' => $data['book_auth'],
                'book_man' => $data['book_man'],
                'book_dec' => $data['book_dec'],
                'book_img' => $data['book_img'],
                'book_num' => $data['book_num'],
                'creat_time' => time(),
                'classify_id'=>$data['classify']

            ]);
            return ['code'=>1,'msg'=>'添加成功'];
        }


        $this->assign('classify', fenlei::all());
        return $this->fetch();
    }


    public function edit()
    {
        $request = Request::instance();
        if ($request->isAjax()) {
            $data = $request->param();
            $validate = new BooksValidate();
            if (!$validate->check($data)) {
                return ['code'=>-1,'msg'=>$validate->getError()];
            }
            $book= BooksModel::get($data['book_id']);
            if ($data['book_img']!=$book->book_img && $book->book_img!='/static/books-img/books.jpg') {
                unlink(ROOT_PATH.'public'.$book->book_id);
            }







            BooksModel::where('book_id', $data['book_id'])->update([
                    'book_name' => $data['book_name'],
                    'book_auth' => $data['book_auth'],
                    'book_man' => $data['book_man'],
                    'book_dec' => $data['book_dec'],
                    'book_img' => $data['book_img'],
                    'book_num' => $data['book_num'],
                    'classify_id' => $data['classify'],
                ]);

            return ['code'=>1,'msg'=>'修改成功'];
        }
        $userinfo= BooksModel::get($request->param('book_id'));

        $this->assign('book_id', $userinfo->book_id);
        $this->assign('book_name', $userinfo->book_name);
        $this->assign('book_auth', $userinfo->book_auth);
        $this->assign('book_man', $userinfo->book_man);
        $this->assign('book_dec', $userinfo->book_dec);
        $this->assign('book_img', $userinfo->book_img);
        $this->assign('book_num', $userinfo->book_num);
        $this->assign('c_id', $userinfo->classify_id);
        $this->assign('classify', fenlei::all());
        return $this->fetch();
    }



    public function upload(){

        $file = $this->request->file('file');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'static/books-img');
        return ['code' => 0, 'msg' => '上传成功', 'data' => ['src' =>'/static/books-img/'.$info->getSaveName()]];
    }

}